/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Java_IO_Buffered_Reader_Writer.java]
 * Creation Date        [07-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_09_io.api;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * java.io.BufferedReader<br>
 * java.io.BufferedWriter<br>
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_IO_Buffered_Reader_Writer {

	/**
	 * 使用BufferedReader & BufferedWriter拷贝文件
	 * 
	 * @param fileSource
	 * @param fileDest
	 */
	public static void copyFileByBufferReaderWriter(String fileSource,
			String fileDest) {
		BufferedReader in = null;
		BufferedWriter out = null;

		try {
			in = new BufferedReader(new FileReader(fileSource));
			out = new BufferedWriter(new FileWriter(fileDest));

			String temp = in.readLine();
			while (temp != null) {
				out.write(new String(temp.getBytes(), "UTF-8"));
				out.newLine();
				temp = in.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 先关闭输出流, 再关闭输入流
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
